class Hooks {
    constructor(){
        this._hooks = {};
    }
    
    addHook(type, fn){
       if (typeof type === "string" && typeof fn === "function") {
            if (typeof this._hooks[type] === "undefined") {
                this._hooks[type] = [fn];
            } else {
                this._hooks[type].push(fn);
            }
        }
        return this; 
    }
    
    dispatch(type,params){
        if (type && this._hooks[type]) {
            let events = {
                type: type,
                target: this,
                params:params
            };
            for (let l = this._hooks[type].length, i=0; i<l; i+=1) {
                this._hooks[type][i].call(this, events);
            }
        }
        return this;
    }
    
    removeHook(type, key){
        let hooks = this._hooks[type];
        if (hooks instanceof Array) {
            if (typeof key === "function") {
                for (let i=0, l=hooks.length; i<l; i+=1){
                    if (hooks[i] === key){
                        hooks.splice(i, 1);
                        break;
                    }
                }
            } else if (key instanceof Array) {
                for (let i=0, l = key.length; i<l; i+=1) {
                    this.removeHook(type, key[i]);
                }
            } else {
                delete this._hooks[type];
            }
        }
        return this;
    }
}

export default Hooks;